const { Buffer } = require('buffer');
const HEADLEN = 16

module.exports = {
    Pack(data,id){
        let header = {
            len:data.length,
            id
        }
        const buf1 = Buffer.from(JSON.stringify(header))
        const buf2 = Buffer.from(data)
        const buf3 = Buffer.concat([buf1,buf2])
        return buf3
    },
    UnPack(binaryData){
        let header = JSON.parse(binaryData.slice(0,HEADLEN).toString())
        let data = {}
        if (header.len > 0){
            let strData = binaryData.slice(HEADLEN,HEADLEN+header.len).toString()
            data.strData = strData
            data.id = header.id
        }
        return data
    }
}